<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Http;

class WxCaptchaImg extends Model
{
	use HasDateTimeFormatter;
    protected $table = 'wx_captcha_img';
    public $timestamps = false;

    protected static function boot()
    {
        parent::boot();

        static::updated(function ($model) {
            // 模型更新时的逻辑
            self::check($model);
        });

        static::created(function ($model) {
            // 模型创建时的逻辑
            self::check($model);
        });
    }

    public static function check(WxCaptchaImg $model){
        $response = Http::get($model->img);
        $imageContents = $response->body();
        $md5 = md5($imageContents);
        if(_empty_($model->md5) || $model->md5 != $md5){
            $model->md5 = $md5;
            $model->save();
        }
    }

}
